import React from 'react';

// 为组件添加onPageShow/onPageHide生命周期
export default function(target) {
    return class extends target {
        $rootElement = null;

        componentDidMount() {
            super.componentDidMount && super.componentDidMount();
            this.$rootElement = $(this.$showHideHook).parents('#Page')[0]; // eslint-disable-line
            if (this.$rootElement) {
                this.$rootElement.showHideList.push(this);
            }
        }

        componentWillUnmount() {
            super.componentWillUnmount && super.componentWillUnmount();
            if (this.$rootElement) {
                this.$rootElement.showHideList = this.$rootElement.showHideList.filter(instance => instance !== this);
            }
        }

        refShowHideHook = d => {
            this.$showHideHook = d;
        };

        render() {
            return <div ref={this.refShowHideHook}>{super.render()}</div>;
        }
    };
}
